//
//  WXPAlreadyAuthGoPayBackView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/10.
//

#import "WXPAlreadyAuthGoPayBackView.h"
#import "WXPOrderDetailVC.h"

@implementation WXPAlreadyAuthGoPayBackView

#pragma mark - lazy
- (UIView *)viewRemind{
    if (_viewRemind == nil) {
        _viewRemind = [UIView new];
        _viewRemind.backgroundColor = COLOR_WHITE;
        [GlobalMethod setRoundView:_viewRemind color:[UIColor whiteColor] numRound:16 width:0];
    }
    return _viewRemind;
}
- (UILabel *)labelRemind{
    if (_labelRemind == nil) {
        _labelRemind = [UILabel new];
        _labelRemind.textColor = COLOR_BLACK;
        _labelRemind.fontNum = 12;
        _labelRemind.numberOfLines = 0;
        _labelRemind.lineSpace = 3;
    }
    return _labelRemind;
}
- (UIView *)viewBack{
    if (_viewBack == nil) {
        _viewBack = [UIView new];
        _viewBack.backgroundColor = COLOR_WHITE;
        _viewBack.userInteractionEnabled = true;
        [GlobalMethod setRoundView:_viewBack color:[UIColor whiteColor] numRound:16 width:0];
    }
    return _viewBack;
}
- (UILabel *)labelTitle{
    if (_labelTitle == nil) {
        _labelTitle = [UILabel new];
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.font = [UIFont boldSystemFontOfSize:16];
    }
    return _labelTitle;
}
- (UIView *)lineTitle{
    if (_lineTitle == nil) {
        _lineTitle = [UIView new];
        _lineTitle.widthHeight = XY(36, 6);
        [GlobalMethod setRoundView:_lineTitle color:[UIColor clearColor] numRound:6/2 width:0];
        _lineTitle.backgroundColor = COLOR_PRIMARY;
    }
    return _lineTitle;
}
- (UIView *)lineView{
    if (_lineView == nil) {
        _lineView = [UIView new];
        _lineView.backgroundColor = COLOR_LINE;
    }
    return _lineView;
}
- (UIView *)viewItemBack{
    if (_viewItemBack == nil) {
        _viewItemBack = [UIView new];
        _viewItemBack.backgroundColor = COLOR_WHITE;
    }
    return _viewItemBack;
}

#pragma mark 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, 0);
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = true;
        
        [self addSubView];
    }
    return self;
}
//添加subview
- (void)addSubView{
    [self addSubview:self.viewRemind];
    [self.viewRemind addSubview:self.labelRemind];
    
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.lineTitle];
    [self.viewBack addSubview:self.labelTitle];
    [self.viewBack addSubview:self.lineView];
    [self.viewBack addSubview:self.viewItemBack];
    [self resetViewWithList:@[]];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    WXPOrderDetailVC * vc = [WXPOrderDetailVC new];
    NSDictionary * dic = self.list[sender.tag];
    ModelOrder * model = [ModelOrder new];
    model.orderNo = [dic stringValueForKey:KEY_orderNo];
    model.status = [dic stringValueForKey:KEY_status];
    vc.orderModel = model;
    [Global_Nav pushViewController:vc animated:true];
}

#pragma mark 刷新view
- (void)resetViewWithList:(NSArray *)list{
    self.list = list;
    // 安全提醒
    self.viewRemind.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, 0);
    NSString * rangeStr = @"STP, Transfer o paynet";
    NSString * contentStr = [NSString stringWithFormat:@"Nuestra aplicación solo acepta pagos a través de %@, por favor, asegúrese de la seguridad de sus fondos.", rangeStr];
    NSRange firstWordRange = [contentStr rangeOfString: rangeStr];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString: contentStr];
    [attributedString addAttribute:NSForegroundColorAttributeName value:COLOR_RED range:firstWordRange];
    NSRange secondWordRange = [contentStr rangeOfString: rangeStr];
    UIFont *boldFont = [UIFont boldSystemFontOfSize:12];
    [attributedString addAttribute:NSFontAttributeName value:boldFont range:secondWordRange];
    self.labelRemind.width = self.viewRemind.width-2*LEFT_MARDIN;
    self.labelRemind.attributedText = attributedString;
    [self.labelRemind sizeToFit];
    self.labelRemind.leftTop = XY(LEFT_MARDIN, LEFT_MARDIN);
    self.viewRemind.height = self.labelRemind.bottom+self.labelRemind.top;
    self.viewRemind.centerXTop = XY(self.width/2, 0);
    self.viewRemind.hidden = false;
    
    // 列表内容
    self.viewBack.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, 0);
    [self.labelTitle fitTitle:@"Aumentar la cuota" variable:0];
    self.labelTitle.leftTop = XY(LEFT_MARDIN, LEFT_MARDIN);
    self.lineTitle.leftBottom = XY(self.labelTitle.left-1, self.labelTitle.bottom);
    self.lineView.widthHeight = XY(self.viewBack.width-2*LEFT_MARDIN, 1);
    self.lineView.centerXTop = XY(self.viewBack.width/2, self.lineTitle.bottom+13);
    
    CGFloat topMark = (self.lineView.bottom+LEFT_MARDIN);
    for (int i = 0; i < list.count; i ++) {
        NSDictionary * dic = list[i];
        UIImageView * image = [UIImageView new];
        image.widthHeight = XY(36, 36);
        [GlobalMethod setRoundView:image color:[UIColor clearColor] numRound:6 width:0];
        image.leftTop =  XY(LEFT_MARDIN, topMark);
        [image sd_setImageWithUrlStr:[dic stringValueForKey:KEY_slogan] placeholderImage:@""];
        [self.viewBack addSubview:image];
        
        UILabel * labelName = [UILabel new];
        [GlobalMethod setLabel:labelName widthLimit:0 fontNum:12 textColor:COLOR_BLACK text:readStr([dic stringValueForKey:KEY_productName]) ];
        labelName.leftTop = XY(image.right+8, image.top);
        [self.viewBack addSubview:labelName];
        
        UILabel * labelContent = [UILabel new];
        labelContent.fontNum = 12;
        labelContent.textColor = COLOR_BLACK;
        NSString * rangeStr = @"+400~1000";
        NSString * contentStr = [NSString stringWithFormat:@"Cuota posterior al reembolso %@", rangeStr];
        NSRange firstWordRange = [contentStr rangeOfString: rangeStr];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString: contentStr];
        [attributedString addAttribute:NSForegroundColorAttributeName value:COLOR_RED range:firstWordRange];
        NSRange secondWordRange = [contentStr rangeOfString: rangeStr];
        UIFont *boldFont = [UIFont boldSystemFontOfSize:16];
        [attributedString addAttribute:NSFontAttributeName value:boldFont range:secondWordRange];
        labelContent.width = self.viewRemind.width-2*LEFT_MARDIN-labelName.left;
        labelContent.attributedText = attributedString;
        [labelContent sizeToFit];
        labelContent.leftTop = XY(labelName.left, labelName.bottom+3);
        [self.viewBack addSubview:labelContent];
        
        UIButton * btnPay = [UIButton new];
        btnPay.titleLabel.fontNum = 14;
        btnPay.backgroundColor = COLOR_PRIMARY;
        [btnPay setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [btnPay setTitle:@"Ir a desembolsar" forState:UIControlStateNormal];
        btnPay.widthHeight = XY(self.viewBack.width-image.left*2, BUTTON_HEIGHT);
        [GlobalMethod setRoundView:btnPay color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
        btnPay.centerXTop = XY(self.viewBack.width/2, labelContent.bottom+LEFT_MARDIN);
        [self.viewBack addSubview:btnPay];
        btnPay.tag = i;
        [btnPay addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        topMark = btnPay.bottom+20;
    }
    
    self.viewBack.height = topMark;
    self.viewBack.centerXTop =  XY(self.width/2, self.viewRemind.hidden ? 0: self.viewRemind.bottom+LEFT_MARDIN);
    self.height = self.viewBack.bottom+LEFT_MARDIN;
}

@end
